package com.source.barcodeMachineRepairAndScrap.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.barcodeMachineRepairAndScrap.entity.BarcodeMachineRepair;
import com.source.barcodeMachineRepairAndScrap.entity.BarcodeMachineScrap;
import com.source.barcodeMachineRepairAndScrap.service.BarcodeMachineScrapService;
import com.source.barcodeMachineRepairAndScrap.vo.BarcodeMachineRepairVO;
import com.source.barcodeMachineRepairAndScrap.vo.BarcodeMachineScrapVO;
import com.source.utils.CommonResult;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author IceBerg777
 * @since 2021-03-07
 */
@Api(tags = "脚标机报废记录管理")
@RestController
@RequestMapping("/barcodeMachineRepairAndScrap/barcode-machine-scrap")
public class BarcodeMachineScrapController {

    @Autowired
    private BarcodeMachineScrapService scrapService;

    @ApiOperation(value = "查询所有报废记录", notes = "分页")
    @GetMapping("/findAllScrapOnPagination")
    public CommonResult<IPage<BarcodeMachineScrapVO>> findAllScrapOnPagination(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                                                @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<BarcodeMachineScrapVO> page = new Page<>(current,size);
        IPage<BarcodeMachineScrapVO> pages = scrapService.findAllScrapOnPagination(page);
        return new CommonResult<IPage<BarcodeMachineScrapVO>>(ResultEnum.SUCCESS,pages);
    }

    @ApiOperation(value = "查询所有报废记录", notes = "分页")
    @GetMapping("/findAllScrap")
    public CommonResult<List<BarcodeMachineScrapVO>> findAllScrap(){
        List<BarcodeMachineScrapVO> list = scrapService.findAllScrap();
        return new CommonResult<List<BarcodeMachineScrapVO>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "模糊查询报废记录", notes = "分页")
    @GetMapping("/findByMultiKeysOnPagination")
    public CommonResult<IPage<BarcodeMachineScrapVO>> findByMultiKeysOnPagination(@RequestParam(value = "keyWord", required = false) String keyWord,
                                                                                   @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                                                   @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if(null == keyWord){
            return findAllScrapOnPagination(current,size);
        }
        Page<BarcodeMachineScrapVO> page = new Page<>(current,size);
        IPage<BarcodeMachineScrapVO> pages = scrapService.findByMultiKeysOnPagination(page, keyWord);
        return new CommonResult<IPage<BarcodeMachineScrapVO>>(ResultEnum.SUCCESS,pages);
    }

    @ApiOperation("根据id查询报废记录")
    @GetMapping("/findById/{id}")
    public CommonResult<BarcodeMachineScrap> findById(@PathVariable String id){
        BarcodeMachineScrap scrap = scrapService.findById(id);
        return new CommonResult<BarcodeMachineScrap>(ResultEnum.SUCCESS,scrap);
    }

    @ApiOperation(value = "根据id修改报废记录")
    @PutMapping("/updateById")
    public CommonResult<String> updateById(@RequestBody BarcodeMachineScrap machineScrap){
        if(scrapService.updateById(machineScrap)){
            return new CommonResult<String>(ResultEnum.SUCCESS);
        }
        return new CommonResult<String>(ResultEnum.ERROR);
    }

    @ApiOperation(value = "增加报废记录")
    @PostMapping("/save")
    public CommonResult<String> save(@RequestBody @Validated BarcodeMachineScrap machineScrap){
        if(scrapService.save(machineScrap)){
            return new CommonResult<String>(ResultEnum.SUCCESS);
        }
        return new CommonResult<String>(ResultEnum.ERROR);
    }

    @ApiOperation(value = "删除报废记录")
    @DeleteMapping("/deleteById/{id}")
    public CommonResult<String> deleteById(@PathVariable(value = "id") String id){
        if(scrapService.removeById(id)){
            return new CommonResult<String>(ResultEnum.SUCCESS);
        }
        return new CommonResult<String>(ResultEnum.ERROR);
    }

}

